const { sqlite3 } = require('../../mysql')

// 管理平台获取留言列表数据
const getScenic = async (ctx) => {
  console.log('ctx.query:', ctx.query)
  let formData = ctx.query,
    offset = (formData.page - 1) * formData.limit,
    keyword = formData.keyword,
    limitdata = {},
    data = {}
  if (keyword) {
    data = await sqlite3('scenic')
      .where('title', keyword)
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = data.length
    console.log('data:', data)
  } else {
    limitdata = await sqlite3('scenic').count({ total: '*' })
    data = await sqlite3('scenic')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = limitdata[0].total
  }
  if (data) {
    // console.log('limitdata:', limitdata)
    // console.log('data:', data)
    ctx.body = {
      code: 200,
      list: data,
      total,
      result: true,
    }
  } else {
    ctx.body = {
      code: false,
    }
  }
}

const getDetail = async (ctx) => {
  let scenicId = ctx.query.id
  data = await sqlite3('scenic')
    .where('id', scenicId)
    .select()
  if (data) {
    let item = data[0]
    item.imgs = item.imgs.split('|')
    ctx.body = {
      code: 200,
      item,
      result: true,
    }
  } else {
    ctx.body = {
      code: false,
    }
  }
}

module.exports = { getScenic, getDetail }
